<template>
  <a-spin :spinning="loading" size="large">
    <Tabs @click-tab="clickTab" sticky swipeable>
      <Tab title="未销号"></Tab>
      <Tab title="已销号"></Tab>
    </Tabs>

    <van-list v-model:loading="loadings" :finished="finished" @load="onLoad" finished-text="没有更多了">
      <div class="record-list">
        <van-cell-group v-for="record in faceList" :key="record.id" class="record-item"
          @click="goDetails(record, urlRoute)">
          <van-cell>
            <!-- 头部信息 -->
            <template #title>
              <div class="record-header">
                <span class="point-name">{{ record.placeName }}</span>
                <van-tag :type="getStatusType(record.eventType)">
                  {{ getStatusText(record.eventType) }}
                </van-tag>
              </div>
            </template>

            <!-- 标签信息 -->
            <template #label>
              <div class="record-info">
                <div class="info-row check-result">
                  <span>问题描述：
                    {{ record.checkContent }}
                  </span>
                </div>
                <div class="info-row">
                  <span>检查人: {{ record.userName }}</span>
                  <span>时间: {{ formatTime(record.recordTime) }}</span>
                </div>
                <div class="info-row" v-if="record.handleMethod || record.handleUserName">
                  <span v-if="record.handleUserName">整改人: {{ record.handleUserName }}</span>
                  <span v-if="record.handleMethod">整改措施: {{ record.handleMethod }}</span>
                </div>
              </div>
            </template>
          </van-cell>
        </van-cell-group>
      </div>
    </van-list>

    <van-tabbar v-model="active">
      <van-tabbar-item icon="home-o" name="list" to="/photo">隐患列表</van-tabbar-item>
      <van-tabbar-item icon="plus" name="add" to="/photoAdd">隐患报送</van-tabbar-item>
    </van-tabbar>
  </a-spin>
</template>

<script setup>
import { ref, onMounted, inject } from "vue"
import { useRoute } from "vue-router";
import { Tab, Tabs, Empty, Tag as VanTag } from "vant";
import router from "@/route/index.js";
import { hiddentroubleList } from "@/api/common.js";
import moment from "moment"

const loading = ref(false),
  loadings = ref(false),
  finished = ref(true),
  toast = inject("toast"),
  route = useRoute();
const faceList = ref([]);
const active = ref('list');
const total = ref(0);
const page = ref(1);
const pageSize = ref(5);
const activeVal = ref(0);
const urlRoute = ref(0);
const userInfo = ref(JSON.parse(localStorage.getItem("SAVE_USER_INFO")));

onMounted(() => {
  getList(0)
})

const formatTime = (time) => {
  if (!time) return ''
  const date = new Date(time)
  return date.toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

function clickTab(val) {
  finished.value = true
  if (val.title == "未销号") {
    getList(0)
    urlRoute.value = 0
  }
  if (val.title == "已销号") {
    getList(3)
    urlRoute.value = 3
  }
}

function goDetails(item, index) {
  router.push("/photoApprove?id=" + item.recordId + "&type=" + index)
}

function getList(val) {
  page.value = 1
  activeVal.value = val
  loading.value = true
  let obj = {
    userId: userInfo.value.userId,
    eventType: activeVal.value,
    page: page.value,
    pageSize: pageSize.value
  }
  hiddentroubleList(obj).then(res => {
    loading.value = false
    faceList.value = res.records
    total.value = res.total
    finished.value = false
  }, err => {
    loading.value = false
  })
}

function onLoad() {
  page.value++
  let obj = {
    userId: userInfo.value.userId,
    eventType: activeVal.value,
    page: page.value,
    pageSize: pageSize.value
  }
  loading.value = true
  hiddentroubleList(obj).then(res => {
    res.records.forEach(item => {
      faceList.value.push(item);
    })
    loading.value = false
    loadings.value = false;
    if (faceList.value.length >= total.value) {
      finished.value = true;
    }
  })
}

  const getStatusType = (status) => {
    const types = {
      '0': 'warning',
      '1': 'primary',
      '2': 'primary',
      '3': 'success'
    }
    return types[status] || 'default'
  }
  
  const getStatusText = (status) => {
    const texts = {
      '0': '待处理',
      '1': '已指派',
      '2': '已整改',
      '3': '已销号'
    }
    return texts[status] || '未知状态'
  }
</script>

<style lang="less" scoped>
:deep(.van-field__label) {
  width: 50vw;
}

:deep(.van-uploader) {
  margin: 8px 0 0 10px;
}

.btn {
  margin: 0 16px;

  button {
    width: 100%;
  }

}

.records {
  min-height: 100vh;
  background-color: #f7f8fa;
}

.record-list {
  padding: 12px;
}

.record-item {
  margin-bottom: 12px;
  border-radius: 8px;
  overflow: hidden;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.point-name {
  font-size: 16px;
  font-weight: 500;
  color: #323233;
  flex: 1;
}

.record-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
  color: #646566;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.check-result {
  margin-bottom: 4px;
}

:deep(.van-cell) {
  padding: 16px;
  line-height: 1.5;
}

:deep(.van-cell) {
  padding: 16px;
  line-height: 1.5;
}

:deep(.van-tag) {
  margin-left: 8px;
}

@font-face {
  font-family: adobe;
  src: url(../../assets/fonts/苹方黑体-中黑-简.woff);
}

:deep(.ant-spin-container) {
  margin-bottom: 60px;
}
</style>